<meta charset="utf-8">
(#) Requires Feature

!!! WARNING: Requires Feature
   This is a warning.

Id
:   `RequiresFeature`
Summary
:   Requires Feature
Severity
:   Warning
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   3.2.0 (September 2018)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/RequiresFeatureDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/RequiresFeatureDetectorTest.kt)

Some APIs require optional features to be present. This check makes sure
that calls to these APIs are surrounded by a check which enforces this.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/CheckFeatures.java:10:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 1 - ERROR
    ----------------
src/test/pkg/CheckFeatures.java:12:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 2 - ERROR
    ----------------
src/test/pkg/CheckFeatures.java:33:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 6 - ERROR - wrong name
    ----------------
src/test/pkg/CheckFeatures.java:39:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 7 - ERROR - inverted logic
    ----------------
src/test/pkg/CheckFeatures.java:52:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    return FeatureChecker.hasFeature("wrong.name") && api.someMethod(); // 10 - ERROR
                                                      ----------------
src/test/pkg/CheckFeatures.java:56:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    return FeatureChecker.hasFeature("wrong.name") || api.someMethod(); // 11 - ERROR
                                                      ----------------
src/test/pkg/CheckFeatures.java:70:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 13 - ERROR: inverted logic in earl exit
    ----------------
src/test/pkg/CheckFeatures.java:92:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 16 - ERROR
    ----------------
src/test/pkg/CheckFeatures.java:99:Warning: someMethod should only be
called if the feature some.name is present; to check call
test.framework.pkg.FeatureChecker#hasFeature [RequiresFeature]
    api.someMethod(); // 17 - ERROR
    ----------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/test/pkg/CheckFeatures.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import test.framework.pkg.FeatureChecker;

@SuppressWarnings({"ClassNameDiffersFromFileName", "unused", "WeakerAccess", "ConstantIfStatement", "MethodMayBeStatic", "ConstantConditions", "StatementWithEmptyBody"})
public class CheckFeatures {
    public static final String SOME_NAME_CONSTANT = "some.name";

    private void testError1(SomeApi api) {
        api.someMethod(); // 1 - ERROR
        if (true) {
            api.someMethod(); // 2 - ERROR
        }
    }

    private void testOkConditional(SomeApi api) {
        if (FeatureChecker.hasFeature("some.name")) {
            api.someMethod(); // 3 - OK
            if (true) {
                api.someMethod(); // 4 - OK
            }
        }
    }

    private void testOkConditionalViaConstant(SomeApi api) {
        if (FeatureChecker.hasFeature(SOME_NAME_CONSTANT)) {
            api.someMethod(); // 5 - OK
        }
    }

    private void testWrongNameConditional(SomeApi api) {
        if (FeatureChecker.hasFeature("wrong.name")) {
            api.someMethod(); // 6 - ERROR - wrong name
        }
    }

    private void testInvertedConditional(SomeApi api) {
        if (!FeatureChecker.hasFeature("some.name")) {
            api.someMethod(); // 7 - ERROR - inverted logic
        }
    }

    private boolean testCorrectAndedExpressions(SomeApi api) {
        return FeatureChecker.hasFeature("some.name") && api.someMethod(); // 8 - OK
    }

    private boolean testCorrectOredExpressions(SomeApi api) {
        return !FeatureChecker.hasFeature("some.name") || api.someMethod(); // 9 - OK
    }

    private boolean testIncorrectAndedExpressions(SomeApi api) {
        return FeatureChecker.hasFeature("wrong.name") && api.someMethod(); // 10 - ERROR
    }

    private boolean testIncorrectOredExpressions(SomeApi api) {
        return FeatureChecker.hasFeature("wrong.name") || api.someMethod(); // 11 - ERROR
    }

    private void testValidEarlyReturn(SomeApi api) {
        if (!FeatureChecker.hasFeature("some.name")) {
            return;
        }
        api.someMethod(); // 12 - OK exited above
    }

    private void testInvalidEarlyReturn(SomeApi api) {
        if (FeatureChecker.hasFeature("some.name")) {
            return;
        }
        api.someMethod(); // 13 - ERROR: inverted logic in earl exit
    }

    public static boolean hasSomeNameFeatureUtilityMethod() {
        return FeatureChecker.hasFeature("some.name");
    }

    public static boolean hasGenericFeatureUtilityMethod(String name) {
        return FeatureChecker.hasFeature(name);
    }

    private void testOkViaSpecificUtilityMethod(SomeApi api) {
        if (hasSomeNameFeatureUtilityMethod()) {
            api.someMethod(); // 14 - OK
        }
    }

    private void testOkViaGenericUtilityMethod(SomeApi api) {
        if (hasGenericFeatureUtilityMethod("some.name")) {
            api.someMethod(); // 15 - OK
        }
        if (hasGenericFeatureUtilityMethod("wrong.name")) {
            api.someMethod(); // 16 - ERROR
        }
    }

    private void testOkConditional(SomeApi api) {
        if (FeatureChecker.hasFeature("some.name")) {
        } else {
            api.someMethod(); // 17 - ERROR
        }
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/test.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import test.framework.pkg.FeatureChecker

fun testLambdas() {
    val api = SomeApi()
    api.applyIfHasFeatureX {
        someMethod() // OK - checked in lambda
    }
    api.applyIfHasFeatureY {
        someMethod() // ERROR - not checked in lambda
    }
}

inline fun <T> T.applyIfHasFeatureX(block: T.() -> Unit): T {
    if (FeatureChecker.hasFeature("some.name")) {
        block()
    }
    return this
}

inline fun <T> T.applyIfHasFeatureY(block: T.() -> Unit): T {
    if (FeatureChecker.hasFeature("some.other.feature")) {
        block()
    }
    return this
}

fun testPropertySyntax() {
    val api = SomeOtherApi()
    if (api.supportsFeatureX) {
        SomeApi().someMethod() // OK - checked via utility method accessed via property syntax
    }
}

class SomeOtherApi {
    val supportsFeatureX get() = FeatureChecker.hasFeature("some.name")
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/framework/pkg/FeatureChecker.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.framework.pkg;

@SuppressWarnings({"ClassNameDiffersFromFileName", "MethodMayBeStatic"})
public class FeatureChecker {
    public static boolean hasFeature(String name) { return true; }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/SomeApi.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import androidx.annotation.RequiresFeature;

@SuppressWarnings({"ClassNameDiffersFromFileName", "MethodMayBeStatic"})
public class SomeApi {
    @RequiresFeature(name = "some.name", enforcement = "test.framework.pkg.FeatureChecker#hasFeature")
    public boolean someMethod() { return true; }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/RequiresFeatureDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `RequiresFeatureDetector.testScenarios`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("RequiresFeature")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("RequiresFeature")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection RequiresFeature
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="RequiresFeature" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'RequiresFeature'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore RequiresFeature ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>